/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:41
 **/




#include "CourseTimeMapper.h"
#include "list"
#include "iostream"
#include "fstream"
#include "sstream"
#include "string"
#include "vector"

using namespace std;
list<CourseTime> CourseTimeMapper::loadCourseTime() {
    list<CourseTime> courseTimeList;
    ifstream ifs("../../resource/CourseTime.txt",ios::in);
    if(!ifs){
        cout<<"CourseTime.txt文件打开失败！"<<endl;
    }
    string line,field;
    while(getline(ifs,line)){
        vector<string> infoList;
        CourseTime courseTime;
        istringstream iss(line);
        while(iss>>field){
            infoList.push_back(field);
        }
        courseTime.setCourseId(infoList[0]);
        courseTime.setStudentId(infoList[1]);
        courseTime.setClassId(infoList[2]);
        courseTime.setWeek(stoi(infoList[3]));
        courseTime.setDay(stoi(infoList[4]));
        courseTime.setSection(stoi(infoList[5]));
        courseTimeList.push_back(courseTime);

    }
    ifs.close();

    return courseTimeList;
}

int CourseTimeMapper::batchInsertCourseTime(list<CourseTime>& courseTimeList){
    ofstream ofs("../../resource/CourseTime.txt");
    if(!ofs){
        cout<<"CourseTime.txt文件打开失败";
    }
    for(CourseTime courseTime:courseTimeList){
        ofs<<courseTime.getCourseId()<<" ";
        ofs<<courseTime.getStudentId()<<" ";
        ofs<<courseTime.getClassId()<<" ";
        ofs<<courseTime.getWeek()<<" ";
        ofs<<courseTime.getSection()<<" ";
    }
    return 0;

}